package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.StorageTblMapper;
import org.example.entity.StorageTbl;
import org.example.service.StorageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StorageServiceImpl implements StorageService {



    private static Logger logger = LoggerFactory.getLogger(StorageServiceImpl.class);

    @Autowired
    private StorageTblMapper storageTblMapper;

    @Override
    public void deduct(String commodityCode, int count) {
        logger.info("开始扣减库存，商品编码：{}，数量：{}", commodityCode, count);
//        StorageTbl storageTbl = storageTblMapper.selectOne(new LambdaQueryWrapper<StorageTbl>().eq(StorageTbl::getCommodityCode, commodityCode));
        StorageTbl storageTbl = storageTblMapper.selectOne(commodityCode);
        int idleCount = storageTbl.getCount() - count;
        if (idleCount < 0) {
            throw new RuntimeException("库存不足");
        }
        storageTbl.setCount(idleCount);
        storageTblMapper.updateById(storageTbl);
        logger.info("库存扣减成功，商品编码：{}，剩余数量：{}", commodityCode, idleCount);
    }
}
